﻿<UserControl x:Class="Resxus.View.ResourceEditView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:model="clr-namespace:Resxus.Model"
    xmlns:vm="clr-namespace:Resxus.ViewModel"
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:local="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
    DataContextChanged="UserControl_DataContextChanged"
    Height="Auto" Width="Auto">    
    <UserControl.Resources>
        
        <Style x:Key="DataGridCheckCellStyle" TargetType="{x:Type my:DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type my:DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter                                           
                                    Margin="0,5,0,5"
                                    Height="Auto"
                                    VerticalAlignment="Center"/>                                                            
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
     
        <Style x:Key="DataGridStyle" TargetType="{x:Type my:DataGrid}">
            <Setter Property="CanUserAddRows" Value="False"/>
            <Setter Property="CanUserDeleteRows" Value="True"/>
            <Setter Property="CanUserReorderColumns" Value="False"/>
            <Setter Property="AlternationCount" Value="2"/>
            <Setter Property="AlternatingRowBackground" Value="LightGray"/>
            <Setter Property="AutoGenerateColumns" Value="False"/>
            <Setter Property="RowHeaderWidth" Value="20"/>
            <Setter Property="GridLinesVisibility" Value="None"/>                        
        </Style>
        
        <ContextMenu x:Key="RecordItemContextMenu">
            <MenuItem Header="리소스 추가" Command="{Binding AddCommand}"/>
            <Separator/>
            <MenuItem Header="리소스 삭제" Command="{Binding DeleteCommand}"/>
            <MenuItem Header="리소스 복사" Command="{Binding CopyCommand}"/>            
            <MenuItem Header="리소스 잘라내기" Command="{Binding CutCommand}"/>
            <MenuItem Header="리소스 붙여넣기" Command="{Binding PasteCommand}"/>            
        </ContextMenu>
        
		<ImageBrush x:Key="Img_Search_TextBox_Background" 
                    AlignmentX="Left"
                    AlignmentY="Center"
                    Stretch="Uniform"
                    ImageSource="/Resxus;Component/Image/Search_TextBox_Background.png"/>
                
    </UserControl.Resources>
    
    <Grid>       
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>            
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>   
        <DockPanel>            
            <StackPanel 
                DockPanel.Dock="Left"                
                VerticalAlignment="Center" 
                Orientation="Horizontal">
                <TextBlock Text="Total : "/>
                <TextBlock Margin="5,0,5,0" Text="{Binding TotalRecords}"/>
                <TextBlock Text="Records"/>
            </StackPanel>
            <StackPanel
                Margin="10,0,0,0"
                DockPanel.Dock="Right"  
                HorizontalAlignment="Right"
                Orientation="Horizontal">
               
                <!-- 바인딩을 변경할 때마다 동작하게 하고 싶음 {Binding XXXXX, UpdateSourceTrigger=PropertyChanged} 형태로 바인딩을 걸어 직접 업데이트
                     비하인드 코드에서 하는 방법 및 자세한 방법은 : http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/911441f7-df8b-4f54-b7eb-ebfd40e88813 -->
                <TextBox                                     
                    x:Name="txtSearch"
                    Text="{Binding SearchText, UpdateSourceTrigger=Explicit}" 
                    KeyUp="txtSearch_KeyUp"                    
                    Margin="3"  
                    Width="200">
					<TextBox.Style>
						<Style TargetType="{x:Type TextBox}">
							<Style.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsFocused" Value="False"/>
                                        <Condition Property="Text" Value=""/>
                                    </MultiTrigger.Conditions>
                                    <MultiTrigger.Setters>
                                        <Setter Property="Background" Value="{StaticResource Img_Search_TextBox_Background}"/>
                                    </MultiTrigger.Setters>                                        
                                </MultiTrigger>                                    
							</Style.Triggers>
						</Style>
					</TextBox.Style>
					</TextBox>
                <StackPanel 
                    Background="Yellow" 
                    VerticalAlignment="Center" 
                    Orientation="Horizontal">
                    <StackPanel.Style>
                        <Style TargetType="{x:Type FrameworkElement}">
                            <Style.Triggers>                                
                                <DataTrigger Binding="{Binding SearchResultsCount}" Value="0">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding SearchText}" Value="">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <TextBlock 
                    Margin="1,0,1,0"                                         
                    Text="{Binding SelectedSearchResultIndex}"/>
                    <TextBlock                     
                        Text="/"/>
                    <TextBlock 
                    Margin="1,0,1,0"                                         
                    Text="{Binding SearchResultsCount}"/>
                </StackPanel>                
                <RepeatButton 
                    Margin="3,3,0,3" 
                    Padding="5,0,5,0"  
                    Content="▲" 
                    Command="{Binding SearchPrevCommand}"/>
                <RepeatButton 
                    Margin="0,3,3,3" 
                    Padding="5,0,5,0"  
                    Content="▼" 
                    Command="{Binding SearchNextCommand}"/>
                <Button 
                    Margin="3" 
                    Padding="8,0,8,0"  
                    Content="고급 검색" 
                    Command="{Binding AdvancedSearchCommand}"/>
            </StackPanel>
        </DockPanel>                  
        <my:DataGrid 
            Grid.Row="1"
            x:Name="RecordGrid"          
            SelectedItem="{Binding SelectedRecord}"
            CellStyle="{StaticResource DataGridCheckCellStyle}"
            Style="{StaticResource DataGridStyle}"  
            ContextMenu="{StaticResource RecordItemContextMenu}"            
            ItemsSource="{Binding Records}">                             
            <my:DataGrid.ItemContainerStyle>
                <Style TargetType="{x:Type my:DataGridRow}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsMatch}" Value="True">
                            <Setter Property="Background" Value="Yellow"/>                            
                        </DataTrigger>                      
                    </Style.Triggers>                    
                </Style>
            </my:DataGrid.ItemContainerStyle>            
            <my:DataGrid.Columns>                                                              
                <my:DataGridTextColumn Header="ID" Width="Auto" Binding="{Binding ID}" CanUserSort="True"/>
                <my:DataGridTextColumn Header="Value" Width="Auto" Binding="{Binding Value}" CanUserSort="True">
                    <my:DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="AcceptsReturn" Value="True"/>
                        </Style>
                    </my:DataGridTextColumn.EditingElementStyle>
                </my:DataGridTextColumn>
            </my:DataGrid.Columns>            
        </my:DataGrid>        
    </Grid>
</UserControl>
